package org.apache.poi.xwpf.converter.core.styles.run;

import org.apache.poi.xwpf.converter.core.styles.XWPFStylesDocument;
import org.apache.poi.xwpf.usermodel.UnderlinePatterns;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTUnderline;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STUnderline.Enum;

import java.util.Objects;

public class RunUnderlineValueProvider extends AbstractRunValueProvider<UnderlinePatterns> {

    /** INSTANCE */
    public static final RunUnderlineValueProvider INSTANCE = new RunUnderlineValueProvider();

    @Override
    public UnderlinePatterns getValue(CTRPr rPr, XWPFStylesDocument stylesDocument) {

        if (Objects.isNull(rPr)) {
            return null;
        }

        if (rPr.isSetU()) {

            CTUnderline ctu = rPr.getU();

            if (Objects.nonNull(ctu)) {

                Enum val = ctu.getVal();

                if (Objects.nonNull(val)) {
                    return UnderlinePatterns.valueOf(val.intValue());
                }
            }
        }
        return null;
    }
}
